package com.ase.test;

/**
 * @author gyhstart
 * @create 2021/2/20 - 22:01
 **/
public class bubbleSort {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,9,8};
        System.out.println(bubbleSort(arr));

    }
    public static int[] bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    // 如果左边的数大于右边的数，则交换，保证右边的数字最大
                    arr[j + 1] = arr[j + 1] + arr[j];
                    arr[j] = arr[j + 1] - arr[j];
                    arr[j + 1] = arr[j + 1] - arr[j];
                }
            }
        }
        return arr;
    }
}
